package win.bigwhale.sudoku.view;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import win.bigwhale.sudoku.GameScene;
import win.bigwhale.sudoku.StartGame;
import win.bigwhale.sudoku.SudokuUtil;
import win.bigwhale.sudoku.controller.GameController;
import win.bigwhale.sudoku.game.Sudoku;
import win.bigwhale.sudoku.game.SudokuFactory;

public class ChooseGame {
	private static ChooseGame chooseGame = new ChooseGame();
	private Stage chooseGameStage = null;
	private Button[] chooseGameGradeButtons = null;
	
	private ChooseGame() {
		chooseGameStage = new Stage();
		VBox vBox = new VBox();
		vBox.setPadding(new Insets(20.0, 40.0, 0.0, 40.0));

		initChooseGameGradeButton();
		addButtonToVBox(vBox);

		Scene mScene = new Scene(vBox);
		chooseGameStage.setScene(mScene);
		chooseGameStage.setTitle(SudokuUtil.CHOOSE_GAME_STAGE_TITLE);
		chooseGameStage.setResizable(false);

		chooseGameStage.setOnCloseRequest(new CloseStageEventHandler());
	}

	private void addButtonToVBox(VBox vBox) {
		Insets buttonInsets = new Insets(0.0, 0.0, 20.0, 0.0);
		for (Button button : chooseGameGradeButtons) {
			VBox.setMargin(button, new Insets(20));
			VBox.setMargin(button, buttonInsets);
			vBox.getChildren().add(button);
			button.setOnMouseClicked(new ChooseGameGradeEventHandler());
		}
	}

	private void initChooseGameGradeButton() {
		chooseGameGradeButtons = new Button[SudokuUtil.GAME_GRADE_COUNT + 1];

		for (int i = 0; i < chooseGameGradeButtons.length; ++i) {
			if (i < SudokuUtil.CHOOSE_GAME_GRADE_BUTTON_TITLES.length) {
				chooseGameGradeButtons[i] = createButtonWithTitleAndSize(SudokuUtil.CHOOSE_GAME_GRADE_BUTTON_TITLES[i]);
			}
		}
	}

	private Button createButtonWithTitleAndSize(String title) {
		Button button  = new Button(title);
		button.setPrefSize(100, 30);
		return button;
	}

	public static ChooseGame getChooseGame() {
		return chooseGame;
	}

	public void show() {
		chooseGameStage.show();
	}

	private final class CloseStageEventHandler implements EventHandler<WindowEvent> {
		@Override
		public void handle(WindowEvent event) {
			if (StartGame.getPrimaryStage().getScene() == GameScene.getGameScene()) {
				GameController.getGameController().resumeGame();
				ControlButtonPane.getControlButtonPane().setAllButtonDisable(false);
			}
		}
	}

	private final class ChooseGameGradeEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			Sudoku sudoku = SudokuFactory.getFactory().createGame(((Button)event.getSource()).getText());
			GameController.getGameController().startGame(sudoku);
			if (StartGame.getPrimaryStage().getScene() == GameScene.getGameScene()) {
				ControlButtonPane.getControlButtonPane().setAllButtonDisable(false);
			}
		}
	}

	public void close() {
		chooseGameStage.close();
	}

}
